package com.cn.longdistancebusstation.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.longdistancebusstation.R;
import com.kaopiz.kprogresshud.KProgressHUD;

/**
 * BaseActivity 中统一处理 Subscription . 防止内存泄漏
 * Created by Administrator on 2018/1/15.
 */

public class BaseActivity extends AppCompatActivity{
    private KProgressHUD mKProgressHUD;
    private TextView mTitleTextView;
    private TextView mMenu1TextView;
    private ImageView mBackImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        registerBroadcast();
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            //
            ActionBar.LayoutParams lp =new ActionBar.LayoutParams(
                    ActionBar.LayoutParams.MATCH_PARENT,
                    ActionBar.LayoutParams.MATCH_PARENT);
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.custom_actionbar, null);
            mBackImageView = (ImageView)view.findViewById(R.id.imageView_back);
            mBackImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            mTitleTextView = (TextView)view.findViewById(R.id.textView_title);
            mMenu1TextView = (TextView)view.findViewById(R.id.textView_menu1);
            //菜单1默认不需要显示
            mMenu1TextView.setVisibility(View.INVISIBLE);
            mMenu1TextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onMenu1Click();
                }
            });
            actionBar.setCustomView(view, lp);
            actionBar.setDisplayShowHomeEnabled(false);//去掉导航
            actionBar.setDisplayShowTitleEnabled(false);//去掉标题
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setDisplayShowCustomEnabled(true);
            Toolbar parent = (Toolbar) view.getParent();
            parent.setContentInsetsAbsolute(0, 0);
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(receiver);
    }

    public void showMenu1(String menuTitle, @DrawableRes int resID){
        mMenu1TextView.setVisibility(View.VISIBLE);
        if(menuTitle.length()>0){
            mMenu1TextView.setText(menuTitle);
        }
        else{
            mMenu1TextView.setText("");
            Drawable leftDrawable = getResources().getDrawable(resID);
            leftDrawable.setBounds(0, 0, 70, 70);
            getMenu1TextView().setCompoundDrawables(leftDrawable, null, null, null);
        }
    }

    /**
     * 标题栏右侧菜单1点击事件
     * */
    public void onMenu1Click(){


    }

    MyReceiver receiver;
    private void registerBroadcast() {
        // 注册广播接收者
        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("exit_app");
        this.registerReceiver(receiver,filter);
    }
    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("exit_app")){
                finish();
            }
        }
    }

    private void createHud(){
        Log.i("BaseActivity", "createHud");
        mKProgressHUD = KProgressHUD.create(BaseActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
    }

    public void showHud(){
        if(mKProgressHUD == null){
            createHud();
        }
        if(!mKProgressHUD.isShowing()){
            mKProgressHUD.show();
            Log.i("BaseActivity", "showHud");
        }
    }

    public void showHud(String hint){
        if(mKProgressHUD == null){
            createHud();
        }
        if(hint.length() > 0){
            mKProgressHUD.setLabel(hint);
        }
        if(!mKProgressHUD.isShowing()){
            mKProgressHUD.show();
            Log.i("BaseActivity", "showHud");
        }
    }

    public void hideHud(){
        Log.i("BaseActivity", "hideHud");
        if(mKProgressHUD != null){
            mKProgressHUD.dismiss();
        }
    }

    public TextView getTitleTextView() {
        return mTitleTextView;
    }

    public void setTitleTextView(TextView titleTextView) {
        mTitleTextView = titleTextView;
    }

    public ImageView getBackImageView() {
        return mBackImageView;
    }

    public TextView getMenu1TextView() {
        return mMenu1TextView;
    }

    public void initEmptyView(int layoutId){
        setContentView(R.layout.activity_no_content);


    }

}

